<?php
// +----------------------------------------------------------------------
// | zhanshop-cloud / Proxydns.php    [ 2024/8/24 10:32 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2011~2024 zhangqiquan All rights reserved.
// +----------------------------------------------------------------------
// | Author: zhangqiquan <768617998@qq.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\api\admin\v1\service\finder;

use app\helper\ProxyDnsShareData;
use zhanshop\App;
use zhanshop\server\Request;
use zhanshop\server\Response;

class Proxydns extends BaseFinder
{
    public function post(Request $request, Response $response){
        $result = parent::post($request, $response);
        $dns = $request->param(0);

        App::callWorker(ProxyDnsShareData::class)->set($dns['domain'], [
            'sock_type' => intval($dns['sock_type']),
            'protocol' => intval($dns['protocol']),
            'address' => $dns['address'],
        ]);

        return $result;
    }

    public function put(Request $request, Response $response)
    {
        $oldDomain = App::database()->model($this->menuData['table_names'][0])->where([$this->menuData['pk'] => $request->get('pk')])->value('domain');
        $dns = $request->param(0);
        if($dns['domain'] != $oldDomain){
            App::error()->setError('domain禁止修改', 403);
        }
        $result = parent::put($request, $response);
        $dns = $request->param(0);

        App::callWorker(ProxyDnsShareData::class)->set($dns['domain'], [
            'sock_type' => intval($dns['sock_type']),
            'protocol' => intval($dns['protocol']),
            'address' => $dns['address'],
        ]);

        return $result;
    }

    public function delete(Request $request, Response $response){
        $input = $request->post();
        $domains = App::database()->model($this->menuData["table_names"][0])->whereIn($this->menuData['pk'], $input['pk'])->column("domain", $this->menuData['pk']);
        $result = parent::delete($request, $response);
        foreach($domains as $domain){
            App::callWorker(ProxyDnsShareData::class)->del($domain);
        }
        return $result;
    }
}